<template>
  <view>
    <view class="goods-item">
      <!-- 左侧盒子 -->
      <view class="goods-item-left">
        <!-- 动态决定check状态 -->
        <radio :checked="goods.goods_state" color="#c00000" v-if="showRodio" @click="radioClickHandler"></radio>
        <image :src="goods.goods_small_logo || defaultPic" class="goods-pic"></image>
      </view>
      <!-- 右侧的盒子 -->
      <view class="goods-item-right">
        <!-- 商品名 -->
        <view class="goods-name">
          {{goods.goods_name}}
        </view>
        <view class="goods-info-box">
          <view class="goods-price">
            ￥{{goods.goods_price | tofixed}}
          </view>
          <!-- 商品数量 -->
          <uni-number-box :min="1" :value="goods.goods_count" v-if="showNum" @change="numChangeHandler"></uni-number-box>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    name:"my-goods",
    props:{
      goods:{
        type: Object,
        default: {}
      },
      showRodio: {
        typeof: Boolean,
        default: false,
      },
      // 是否展示价格右侧的 NumberBox 组件
      showNum: {
        type: Boolean,
        default: false,
      },
    },
    data() {
      return {
        // 默认的空图片
        defaultPic: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.51yuansu.com%2Fpic3%2Fcover%2F01%2F55%2F70%2F594c83b24e57c_610.jpg&refer=http%3A%2F%2Fpic.51yuansu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1657160042&t=9c46b3a9897030afab8da8177a22e5e5'
      };
    },
    filters:{
      tofixed(num){
        return Number(num).toFixed(2)
      }
    },
    methods:{
      // radio组件的点击事件
      radioClickHandler(){
        this.$emit('radio-change',{
          goods_id: this.goods.goods_id,
          // 这里修改了状态 但是并没有修改vuex里的数据
          goods_state: !this.goods.goods_state
        })
      },
      // 监听NumberBox数量变化
      // NumberBox 组件的 change 事件处理函数
      numChangeHandler(val){
        // 通过 this.$emit() 触发外界通过 @ 绑定的 num-change 事件
        this.$emit('Num-change',{
          // 商品的 Id
          goods_id: this.goods.goods_id,
          // 商品的最新数量
          goods_count: +val
        })
      }
    }
  }
</script>

<style lang="scss">
.goods-item {
  display: flex;
  padding: 10px 5px;
  border-bottom: 1px solid #f0f0f0;
  width: 750rpx;
  box-sizing: border-box;
  .goods-item-left {
    margin-right: 5px;
    display: flex;
    justify-content: space-around;
    align-items: center;
    .goods-pic {
      width: 100px;
      height: 100px;
      display: block;
    }
  }

  .goods-item-right {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    flex: 1;
    .goods-name {
      font-size: 13px;
    }
    .goods-info-box{
      display: flex;
      justify-content: space-between;
      align-items: center;
      .goods-price {
        font-size: 16px;
        color: #c00000;
      }
    }
  }
}
</style>